package com.hackaton.discounts;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class AdressView extends MapActivity {
	
	private MapView map;
	private MapController mc;
	private MyLocationOverlay me;
	
	List<Overlay> mapOverlays;
	Drawable drawable;
	DiscountItemizedOverlay itemizedOverlay;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		setContentView(R.layout.map);
		
		map = (MapView) findViewById(R.id.mapview);
		map.setBuiltInZoomControls(true);
		
		mc = map.getController();
		
		
		mapOverlays = map.getOverlays();
		drawable = this.getResources().getDrawable(R.drawable.icon);
		itemizedOverlay = new DiscountItemizedOverlay(drawable);
		
//		me=new MyLocationOverlay(this, map);
//		map.getOverlays().add(me);
		
		//initLocation();
		initCiklum();
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	public void initLocation(){
    	// Acquire a reference to the system Location Manager
    	LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

//    	LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
    	// Or use LocationManager.GPS_PROVIDER

    	//Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
    	
    	// Define a listener that responds to location updates
    	LocationListener locationListener = new LocationListener() {
    	    public void onLocationChanged(Location location) {
    	      // Called when a new location is found by the network location provider.
    	    	gotoLocation(location);
    	    }

    	    public void onStatusChanged(String provider, int status, Bundle extras) {}

    	    public void onProviderEnabled(String provider) {}

    	    public void onProviderDisabled(String provider) {}
    	  };

    	// Register the listener with the Location Manager to receive location updates
    	locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }
	
	public void gotoLocation(Location location) {		

		double lat = location.getLatitude();
		double lon = location.getLongitude();

		GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
		
		OverlayItem overlayitem = new OverlayItem(point, "", "");
		itemizedOverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedOverlay);
		
		
		mc.animateTo(point);
		mc.zoomIn();

//		if (!map.isSatellite())
//			map.toggleSatellite();

		map.invalidate();
	}
	
	public void initCiklum(){
		
		//-122.242135,47.627787
		//-77.036519,38.897605
		double lat = 50.423526; //location.getLatitude();
		double lon = 30.505105; //location.getLongitude();
		
		try{
			GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
		
			OverlayItem overlayitem = new OverlayItem(point, "", "");
			itemizedOverlay.addOverlay(overlayitem);
			mapOverlays.add(itemizedOverlay);
		
		
			mc.animateTo(point);
			mc.zoomIn();
		
		}catch (Exception e) {
			Log.e("point", e.toString());
		}
//		if (!map.isSatellite())
//			map.toggleSatellite();

		map.invalidate();
	}
	
}